package com.monsterPatties.utils.soundManager 
{		
	import com.greensock.TweenLite;	
	import com.greensock.easing.*;	

	import com.monsterPatties.utils.soundManager.config.SoundManagerConfig;	
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.utils.getTimer;
	/**
	 * ...
	 * @author ...
	 */
	public class SoundManager
	{
		/*-------------------------------------------------------------------Constant---------------------------------------------------------------------*/
		
		
		/*-------------------------------------------------------------------Properties---------------------------------------------------------------------*/
		private static var _instance:SoundManager;
		
		private var _bgMusicSound:Sound;	
		private var _soundChannelBg:SoundChannel;
		private var _bgSoundTransForm:SoundTransform;
		private var _loopBgMusic:Boolean;	
		
		private var _soundEffect:Sound;
		private var _soundEffectChannel_1:SoundChannel;
		private var _sfxSoundTransForm:SoundTransform;
		private var _isSfxLoop:Boolean;
		
		private var _bgMusicSound2:Sound;	
		private var _soundChannelBg2:SoundChannel;
		private var _bgSoundTransForm2:SoundTransform;
		private var _loopBgMusic2:Boolean;	
		private var _whichBgMusic2:int;
		
		private var _whichBgMusic:int;
		private var _whichEffect:int;	
		
		private var _tempTime:int;
		private var _timeLapse:Number;
		private var _delay:Boolean;
		
		public var sfxStillPlaying:Boolean;
		
		//new
		private var _playingSfx:int;
		
		private var _muteSfx:Boolean;
		private var _muteBgm:Boolean;
		private var _muteBgm2:Boolean;
		
		//
		private var _volObj:Object;
		private var _bgVol:Number = 1;
		private var _bgVol2:Number= 1;
		
		/*-------------------------------------------------------------------Constructor---------------------------------------------------------------------*/	
		
		public function SoundManager ( enforcer:SingletonEnforcer ) {
			init();
		}
			
		
		public static function getInstance():SoundManager { 
			if ( SoundManager._instance == null ) {
				SoundManager._instance = new SoundManager( new SingletonEnforcer() );
			}			
			return SoundManager._instance;			
		}	
		
		private function init( ):void 
		{	
			_volObj = new Object();			
			prepareTransform();
			trace( "SoundManagerLoaded" );			
		}
		
		public function destroy():void 
		{			
			trace( "Sound Manager Garbage Has been collected!" );
		}
		
		/*-------------------------------------------------------------------Methods---------------------------------------------------------------------*/	
		
		private function prepareTransform():void 
		{
			_sfxSoundTransForm = new SoundTransform();
			_bgSoundTransForm = new SoundTransform();
			_bgSoundTransForm2 = new SoundTransform();			
		}
		
		public function selectBgMusic( whichBgMusic:int, loop:Boolean = true ,vol:Number = 1 ):void 
		{
			_whichBgMusic = whichBgMusic;
			_loopBgMusic = loop;
			_bgVol = vol;
			_volObj.volume = vol;
			_bgSoundTransForm.volume = _bgVol;
			_volObj.volume = 1;
			
			if( whichBgMusic <  SoundManagerConfig.BGM_LIST.length && whichBgMusic > -1 ){				
				_bgMusicSound = SoundManagerConfig.BGM_LIST[ whichBgMusic ];
			}else {
				throw( "SoundManagerError: Invalid selection of Bgm!" );
			}
		}
		
		
		public function playBgMusic( ):void
		{			
			if( _bgMusicSound != null && !_muteBgm ){
				stopBgMusic();				
				_soundChannelBg = _bgMusicSound.play();				
				_soundChannelBg.soundTransform = _bgSoundTransForm;
				_soundChannelBg.addEventListener( Event.SOUND_COMPLETE, onBgMusicComplete );				
			}
		}
		
		public function  fadeInBgSound():void 
		{			
			TweenLite.to( _volObj, 3, { volume:0, onUpdate:function ():void { 
				_bgSoundTransForm.volume = _volObj.volume;
				_soundChannelBg.soundTransform = _bgSoundTransForm;
				//_soundChannelBg.soundTransform = new SoundTransform( _volObj.volume );
				//trace( "fading: " ,_bgSoundTransForm.volume);
				}, onComplete:function():void {	}
			} );			
		}		
		
		public function stopBgMusic():void 
		{	
			if( _soundChannelBg != null && _bgMusicSound != null ){
				_soundChannelBg.stop();				
			}	
		}
		
		public function removeBgSound():void 
		{
			stopBgMusic();
			if( _soundChannelBg!= null ){
				_soundChannelBg.removeEventListener( Event.SOUND_COMPLETE, onBgMusicComplete );
				_bgMusicSound = null;
			}
		}
		
		
		
		///new
		public function selectBgMusic2( whichBgMusic:int, loop:Boolean = true ):void 
		{
			_whichBgMusic2 = whichBgMusic;
			_loopBgMusic2 = loop;
			
			if( whichBgMusic <  SoundManagerConfig.BGM_LIST.length && whichBgMusic > -1 ){				
				_bgMusicSound2 = SoundManagerConfig.BGM_LIST[ whichBgMusic ];
			}else {
				throw( "SoundManagerError: Invalid selection of Bgm2!" );
			}
		}
		
		
		public function playBgMusic2( ):void
		{			
			if( _bgMusicSound2 != null && !_muteBgm2 ){
				stopBgMusic2();
				_soundChannelBg2 = _bgMusicSound2.play();
				_soundChannelBg2.soundTransform = _bgSoundTransForm2;
				_soundChannelBg2.addEventListener( Event.SOUND_COMPLETE, onBgMusic2Complete );				
			}
		}
		
		public function stopBgMusic2():void 
		{	
			if( _soundChannelBg2 != null && _bgMusicSound2 != null ){
				_soundChannelBg2.stop();				
			}	
		}
		
		public function removeBgSound2():void 
		{
			stopBgMusic2();
			if( _soundChannelBg2 != null ){
				_soundChannelBg2.removeEventListener( Event.SOUND_COMPLETE, onBgMusic2Complete );
				_bgMusicSound2 = null;
			}			
		}	
		
		
		public function selectSoundEffect( whichEffect:int, loop:Boolean = false ):void 
		{	
			_isSfxLoop = loop;
			
			if( whichEffect < SoundManagerConfig.SOUND_EFFECT_LIST.length && whichEffect > -1 ){
				_whichEffect = whichEffect;
				_soundEffect = SoundManagerConfig.SOUND_EFFECT_LIST[ _whichEffect ];
			}else {
				throw( "SoundManagerError: Invalid selection of sfx!" );
			}
		}
		
		private function playEffect( ):void 
		{
			//if ( _playingSfx + 1 < 30 ){
				if ( _sfxSoundTransForm != null ) {
					_playingSfx++;
					_soundEffectChannel_1 = _soundEffect.play();
					//_soundEffectChannel_1.soundTransform = _sfxSoundTransForm;					
					//_soundEffectChannel_1.addEventListener( Event.SOUND_COMPLETE, onSoundEffectComplete );	
				}				
			//}
		}
		
		public function playSoundEffect( delay:Boolean = false, volume:Number = 1 ):void
		{	
			_sfxSoundTransForm.volume = volume;
			_timeLapse = ( ( getTimer() / 1000 ) - ( _tempTime / 1000 ) );	
			
			if ( _soundEffect != null && !_muteSfx ){
				if ( delay ) {
					if(  !sfxStillPlaying ){
						trace( "playefect222" );					
						playEffect();
						sfxStillPlaying = true;
					}
				}else {					
					playEffect();
				}
				
				
			}
		}
		
		public function stopSoundEffect():void 
		{	
			if( _soundEffectChannel_1 != null && _soundEffect != null ){
				_soundEffectChannel_1.stop();				
			}	
		}
		
		public function removeSoundEffect():void 
		{
			//if( _soundEffectChannel_1 != null && _soundEffect != null ){
				//stopSoundEffect();
				//_soundEffectChannel_1.removeEventListener( Event.SOUND_COMPLETE, onSoundEffectComplete );
				//_soundEffect = null;
			//}
		}
		
		
		public function bgmVolume( vol:Number ):void 
		{			
			_bgVol = vol;
			_volObj.volume = vol;
			_bgSoundTransForm.volume = _bgVol;
			_soundChannelBg.soundTransform = _bgSoundTransForm;
		}		
		
		
		public function sfxVolume( vol:Number = 1 ):void 
		{			
			_sfxSoundTransForm.volume = vol;			
		}		
		
		
		public function stopAllSound():void 
		{
			stopBgMusic();
			stopBgMusic2();
			stopSoundEffect();			
		}
		
		public function playAllSound():void 
		{
			playBgMusic();
			playSoundEffect();
		}
		
		public function muteSfx():void 
		{
			_muteSfx = true;
		}
		
		public function unMuteSfx():void 
		{
			_muteSfx = false;
		}
		
		public function muteBgm():void 
		{
			_muteBgm = true;
		}
		
		public function unMuteBgm():void 
		{
			_muteBgm = false;
		}
		
		public function muteBgm2():void 
		{
			_muteBgm2 = true;
		}
		
		public function unMuteBgm2():void 
		{
			_muteBgm2 = false;
		}		
		
		/*-------------------------------------------------------------------Getters---------------------------------------------------------------------*/		
		
		/*-------------------------------------------------------------------Setters---------------------------------------------------------------------*/
		
		/*-------------------------------------------------------------------EventHandlers---------------------------------------------------------------------*/
		
		private function onBgMusicComplete ( e:Event ):void 
		{			
			//selectBgMusic( _whichBgMusic + 1  );
			trace( "bg1Complte!" );			
			if( _loopBgMusic ){
				playBgMusic( );
			}
			//trace( "bgEnd sound!.." );
		}
		
		private function onBgMusic2Complete ( e:Event ):void 
		{			
			if( _loopBgMusic2 ){
				playBgMusic2( );
			}			
		}
		
		private function onSoundEffectComplete ( e:Event  ):void 
		{			
			//trace( "SoundEffect! complete!" );
			sfxStillPlaying = false;
			_tempTime = getTimer();	
			_playingSfx--;
		}	
		
	}
}

class SingletonEnforcer { }
